package nowcoder.hw.hj101;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

//3868643487
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        Integer numberCnt = Integer.valueOf(reader.readLine());
        String numbers = reader.readLine();
        String sortType =reader.readLine();

        List<Integer> linkedList = new LinkedList<>();
        for (String s : numbers.split(" ")) {
            Integer e = Integer.valueOf(s);
            int pos=linkedList.size();
            if (!linkedList.isEmpty()){
                for (int i = 0; i < linkedList.size(); i++) {
                    if (e<=linkedList.get(i)){
                        pos=i;
                        break;
                    }
                }
                if (pos==linkedList.size()){
                    linkedList.add(e);
                }else
                    linkedList.add(pos,e);
            }else {
                linkedList.add(e);
            }

        }
        StringBuilder sb =new StringBuilder();
        if (sortType.equals("0")){
            for (int i = 0; i < linkedList.size(); i++) {
                sb.append(linkedList.get(i)+" ");
            }
        }
        else if (sortType.equals("1")){
            for (int i = linkedList.size()-1; i >=0; i--) {
                sb.append(linkedList.get(i)+" ");
            }
        }
        System.out.println(sb.deleteCharAt(sb.length()-1).toString());
    }


}